layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //学校列表
    var tableIns = table.render({
        elem: '#schoolList',
        url : '/schoolList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        id : "schoolListTable",
        cols : [[
        {type: "checkbox", fixed:"left", width:50},
        {type: "numbers", title: '序号',fixed:"left", width:50},
        {field: 'id', title: 'Id', minWidth:0, width:0,hide:true},
        {field: 'name', title: '名称', minWidth:80, align:"center"},
        {field: 'short_name', title: '简称', minWidth:150, align:'center'},
        {field: 'province_name', title: '所在省份', minWidth:120, align:'center'},
        {field: 'city_name', title: '所在城市', minWidth:120, align:'center'},
        {field: 'area_name', title: '所在县区', minWidth:120, align:'center'},
        {field: 'admin_name', title: '管理员', minWidth:120, align:'center'},
        {title: '操作', minWidth:190,fixed:"right",align:"center",templet:"#schoolListBar"}
        ]],
        done: function () {
            // $("[data-field='id']").css('display','none');
        }
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("schoolListTable",{
                url:"/schoolList",
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    school_name: $(".searchVal").val()  //搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加学校
    function addSchool(edit){
        var title = "添加学校";
        var url = "/toSchoolAdd";
        if (edit) {
            title = "编辑学校";
            url="/toSchoolModify?id="+edit.id;
        }
        var index = layui.layer.open({
            title : title,
            type : 2,
            content : url,
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回学校列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }
    $(".addNews_btn").click(function(){
        addSchool();
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('schoolListTable'),
            data = checkStatus.data,
            id = "";
        if(data.length > 0) {
            for (var i in data) {
                if(i==data.length-1){
                    id+=data[i].id;
                }else{
                    id+=data[i].id+",";
                }
            }
            layer.confirm('确定删除选中的学校？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("/schoolDel",{
                    id : id //将需要删除的newsId作为参数传入
                },function(data){
                    if (data =="success") {
                        tableIns.reload();
                        layer.close(index);
                    }else if (data == "error") {
                        layer.msg("学校删除失败！");
                    }else if(data=="isExistBj"){
                        layer.msg("学校下存在班级，删除失败！");
                    }
                })
            })
        }else{
            layer.msg("请选择需要删除的学校");
        }
    });

    //列表操作
    table.on('tool(schoolList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addSchool(data);
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此学校？',{icon:3, title:'提示信息'},function(index){
                $.get("/schoolDel",{
                    id : data.id //参数传入
                },function(data){
                    if (data =="success") {
                        tableIns.reload();
                        layer.close(index);
                    }else if (data == "error") {
                        layer.msg("学校删除失败！");
                    }else if(data=="isExistBj"){
                        layer.msg("学校下存在班级，删除失败！");
                    }

                })
            });
        }/*else if (layEvent==='viewBj') {

        }*/
    });
/*    document.onkeydown = function(e){
        var ev = document.all ? window.event : e;
        if(ev.keyCode==13) {
            table.reload("schoolListTable",{
                url:"/schoolList",
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    school_name: $(".searchVal").val()  //搜索的关键字
                }
            })
        }
    }*/
})
